package com.gao.controller;

import com.gao.annontation.StarAuthority;
import com.gao.entity.Product;
import com.gao.service.ProductService;
import com.gao.util.ResultVO;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author gao
 * @time 2022/11/25 11:23:34
 */
@RestController
@RequiredArgsConstructor
public class ProductController {

    @NonNull
    private ProductService productService;

    @RequestMapping("/products/{productId}")
    public ResultVO find(@PathVariable Integer productId) {
        Product product = productService.findByProductId(productId);
        return ResultVO.success("查询成功", product);
    }

    @RequestMapping("/products/findByProductIds")
    public ResultVO findByProductIds(@RequestBody List<Integer> productIds) {
        List<Product> products = productService.findByProductIds(productIds);
        return ResultVO.success("查询成功", products);
    }

    @StarAuthority("product:update")
    @RequestMapping("/products/deductStock/{productId}/{quantity}")
    public ResultVO deductStock(@PathVariable Integer productId, @PathVariable Integer quantity) {
        Integer stock = productService.deductStock(productId, quantity);
        return ResultVO.success("扣减库存成功，剩余库存：" + stock);
    }

}